package com.loongtech.bi.action.advertisment;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.advertisment.controller.BaseAdvAction;
import com.loongtech.bi.advertisment.model.AdvOverviewData;
import com.loongtech.bi.advertisment.model.chart.*;
import com.loongtech.bi.advertisment.service.AdvOverviewService;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * ClassName: AdvDayDataAction
 * 
 * @Description: TODO 市场数据分析 —— 广告投放概览
 * @author liuchen
 * @date 2017-06-15 下午2:23:49
 */
@Controller
@Scope("prototype")
@RequestMapping("/market/advOverviewReportMarket")
public class AdvOverviewReportBiAction extends BaseAdvAction {
	private static final long serialVersionUID = 1L;
    @Autowired
    private SysUserManager sysUserManager;

	@Resource(name = "advOverviewService")
	private AdvOverviewService service;

	private AdvOverviewData advOverviewData;
	private List<ChartUIData> advChart; // 数据趋势图表
	private List<ChartUIData> effectChart; // 效果数据图表
	private List<ChartUIData> topChart; // TOP指标图表

	@RequestMapping(value = "initParam.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void initParam(HttpServletRequest request, HttpServletResponse response, @RequestParam String uid, String callback) {
		try {
            Map<String, Object> result = new HashMap<String, Object>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

			result.put("res", "0");
			result.put("platform", getPlatform());
			result.put("agent", getAgent());
			result.put("paytype", getPaytype());
			result.put("channelName", getChannelName());
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}

	@RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String sday,
			@RequestParam String eday, @RequestParam String agent, @RequestParam String platform,
			@RequestParam String channelName, @RequestParam String paytype, @RequestParam String uid, String callback) {
		try {
            Map<String, Object> result = new HashMap<String, Object>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

			if (hasAgents == null || hasAgents.size() <= 0) {
				result.put("res", "1");
				result.put("msg", "此用户无任何代理商权限，请联系管理员配置!");
				printToJson(response, result, callback);
				return;
			}

            String[] channelNames = "".equals(channelName) ? getChannelName().toArray(new String[getChannelName().size()]) : Utils.matchesString(channelName).split(",");
            String[] agents = "".equals(agent) ? getAgent().toArray(new String[getAgent().size()]) : Utils.matchesString(agent).split(",");
            String[] platforms = "".equals(platform) ? getPlatform().toArray(new String[getPlatform().size()]) : Utils.matchesString(platform).split(",");
            String[] paytypes = "".equals(paytype) ? getPaytype().toArray(new String[getPaytype().size()]) : Utils.matchesString(paytype).split(",");

            advChart = new ArrayList<>();
			effectChart = new ArrayList<>();
			topChart = new ArrayList<>();

			advOverviewData = service.getActiveTranDatas(sday, eday, channelNames, agents, platforms, paytypes);
			advOverviewData.setNewPay(UIHelper.getYuan(advOverviewData.getNewPay()));


			List<ClickChartData> clickChartData = service.getClickChartUIData(sday, eday, channelNames, agents,
					platforms, paytypes);

			if (clickChartData != null && clickChartData.size() > 0) {
				ChartUIData payClassChart0 = new ChartUIData();
				payClassChart0 = new ChartUIData();
				payClassChart0.setJsId("0");
				payClassChart0.setJsonType(ChartType.SPLINE.type);
				payClassChart0.setJsonStr(new ChartTool(clickChartData, ClickChartData.class, "")
						.loadData(payClassChart0.getJsonType()));
				advChart.add(payClassChart0);
			}

			List<ActiveChartData> acitveChartData = service.getAcitveChartUIData(sday, eday, channelNames, agents,
					platforms, paytypes);
			if (acitveChartData != null && acitveChartData.size() > 0) {
				ChartUIData payClassChart1 = new ChartUIData();
				payClassChart1 = new ChartUIData();
				payClassChart1.setJsId("1");
				payClassChart1.setJsonType(ChartType.SPLINE.type);
				payClassChart1.setJsonStr(new ChartTool(acitveChartData, ActiveChartData.class, "")
						.loadData(payClassChart1.getJsonType()));
				advChart.add(payClassChart1);
			}

			List<NewAccountChartData> newAccountChartData = service.getNewAccountChartUIData(sday, eday, channelNames,
					agents, platforms, paytypes);
			if (newAccountChartData != null && newAccountChartData.size() > 0) {
				ChartUIData payClassChart2 = new ChartUIData();
				payClassChart2 = new ChartUIData();
				payClassChart2.setJsId("2");
				payClassChart2.setJsonType(ChartType.SPLINE.type);
				payClassChart2.setJsonStr(new ChartTool(newAccountChartData, NewAccountChartData.class, "")
						.loadData(payClassChart2.getJsonType()));
				advChart.add(payClassChart2);
			}

			List<PayChartData> payChartData = service.getPayChartUIData(sday, eday, channelNames, agents, platforms,
					paytypes);
			if (payChartData != null && payChartData.size() > 0) {
				ChartUIData payClassChart3 = new ChartUIData();
				payClassChart3 = new ChartUIData();
				payClassChart3.setJsId("3");
				payClassChart3.setJsonType(ChartType.SPLINE.type);
				payClassChart3.setJsonStr(new ChartTool(payChartData, PayChartData.class, "").loadData(payClassChart3
						.getJsonType()));
				advChart.add(payClassChart3);
			}

			List<ClickActiveRateData> clickActiveRateData = service.getClickActiveRateUIData(sday, eday, channelNames,
					agents, platforms, paytypes);
			if (payChartData != null && payChartData.size() > 0) {
				ChartUIData payClassChart4 = new ChartUIData();
				payClassChart4 = new ChartUIData();
				payClassChart4.setJsId("4");
				payClassChart4.setJsonType(ChartType.SPLINE.type);
				payClassChart4.setJsonStr(new ChartTool(clickActiveRateData, ClickActiveRateData.class, "")
						.loadData(payClassChart4.getJsonType()));
				advChart.add(payClassChart4);
			}

			List<ActiveNewRateData> activeNewRateData = service.getActiveNewRateUIData(sday, eday, channelNames,
					agents, platforms, paytypes);
			if (payChartData != null && payChartData.size() > 0) {
				ChartUIData payClassChart5 = new ChartUIData();
				payClassChart5 = new ChartUIData();
				payClassChart5.setJsId("5");
				payClassChart5.setJsonType(ChartType.SPLINE.type);
				payClassChart5.setJsonStr(new ChartTool(activeNewRateData, ActiveNewRateData.class, "")
						.loadData(payClassChart5.getJsonType()));
				advChart.add(payClassChart5);
			}

			List<DiscountData> discountData = service.getDiscountUIData(sday, eday, channelNames, agents, platforms,
					paytypes);
			if (discountData != null && discountData.size() > 0) {
				ChartUIData payClassChart1 = new ChartUIData();
				payClassChart1 = new ChartUIData();
				payClassChart1.setJsId("6");
				payClassChart1.setJsonType(ChartType.SPLINE.type);
				payClassChart1.setJsonStr(new ChartTool(discountData, DiscountData.class, "").loadData(payClassChart1
						.getJsonType()));
				effectChart.add(payClassChart1);
			}

			List<RemainData> remainData = service
					.getRemainUIData(sday, eday, channelNames, agents, platforms, paytypes);
			if (remainData != null && remainData.size() > 0) {
				ChartUIData payClassChart2 = new ChartUIData();
				payClassChart2 = new ChartUIData();
				payClassChart2.setJsId("7");
				payClassChart2.setJsonType(ChartType.SPLINE.type);
				payClassChart2.setJsonStr(new ChartTool(remainData, RemainData.class, "").loadData(payClassChart2
						.getJsonType()));
				effectChart.add(payClassChart2);
			}

			List<LtvData> ltvData = service.getLtvUIData(sday, eday, channelNames, agents, platforms, paytypes);
			if (ltvData != null && ltvData.size() > 0) {
				ChartUIData payClassChart3 = new ChartUIData();
				payClassChart3 = new ChartUIData();
				payClassChart3.setJsId("8");
				payClassChart3.setJsonType(ChartType.SPLINE.type);
				payClassChart3.setJsonStr(new ChartTool(ltvData, LtvData.class, "").loadData(payClassChart3
						.getJsonType()));
				effectChart.add(payClassChart3);
			}

			List<TopClickData> topClickData = service.getTopClickUIData(sday, eday);
			if (topClickData != null && topClickData.size() > 0) {
				ChartUIData payClassChart1 = new ChartUIData();
				payClassChart1 = new ChartUIData();
				payClassChart1.setJsId("9");
				payClassChart1.setJsonType(ChartType.BAR2.type);
				payClassChart1.setJsonStr(new ChartTool(topClickData, TopClickData.class, "").loadData(payClassChart1
						.getJsonType()));
				topChart.add(payClassChart1);
			}

			List<TopActiveData> topActiveData = service.getTopActiveUIData(sday, eday);
			if (topActiveData != null && topActiveData.size() > 0) {
				ChartUIData payClassChart2 = new ChartUIData();
				payClassChart2 = new ChartUIData();
				payClassChart2.setJsId("10");
				payClassChart2.setJsonType(ChartType.BAR2.type);
				payClassChart2.setJsonStr(new ChartTool(topActiveData, TopActiveData.class, "").loadData(payClassChart2
						.getJsonType()));
				topChart.add(payClassChart2);
			}

			List<TopPayData> topPayData = service.getTopPayUIData(sday, eday);
			if (topPayData != null && topPayData.size() > 0) {
				ChartUIData payClassChart3 = new ChartUIData();
				payClassChart3 = new ChartUIData();
				payClassChart3.setJsId("11");
				payClassChart3.setJsonType(ChartType.BAR2.type);
				payClassChart3.setJsonStr(new ChartTool(topPayData, TopPayData.class, "").loadData(payClassChart3
						.getJsonType()));
				topChart.add(payClassChart3);
			}

			result.put("res", "0");
			result.put("advOverviewData", advOverviewData);
			result.put("advChart", advChart);
			result.put("effectChart", effectChart);
			result.put("topChart", topChart);
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}
}
